package net.qiqb.module;

import java.util.*;

public class ModuleContext {


    private final Map<String, ModuleServiceConfig> interfaceCache = new HashMap<>();

    private List<ModuleServiceDispatcher> serviceDispatchers = new ArrayList<>();

    /**
     * 添加模块接口
     *
     * @param moduleInterface
     */
    public synchronized void addModuleInterface(String moduleInterface) {
        if (interfaceCache.get(moduleInterface) == null) {
            interfaceCache.put(moduleInterface, new ModuleServiceConfig(this, moduleInterface));
        }
    }

    public void setLocalImpl(String moduleInterface, String moduleServiceClassName) {
        final ModuleServiceConfig serviceConfig = interfaceCache.get(moduleInterface);
        if (serviceConfig == null) {
            addModuleInterface(moduleInterface);
        }
        interfaceCache.get(moduleInterface).setLocalImplClass(moduleServiceClassName);
    }

    public ModuleServiceConfig getServiceConfig(String moduleInterface) {

        return interfaceCache.get(moduleInterface);
    }

    public Collection<ModuleServiceConfig> getServiceConfigs() {
        return this.interfaceCache.values();
    }

    public List<ModuleServiceDispatcher> getServiceDispatchers() {
        return serviceDispatchers;
    }

    public void addServiceDispatcher(ModuleServiceDispatcher serviceDispatcher) {
        this.serviceDispatchers.add(serviceDispatcher);
    }
}
